define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
	const ids = Fast.api.query('ids');

	const controller = {
    index() {
      // 设置添加窗口的大小
      $(".btn-add").data('area', ['90%', '90%'])
      
			Table.api.init({
				extend: {
					index_url: 'subject/chapter/index',
					add_url: 'subject/chapter/add?subid=' + ids,
					edit_url: 'subject/chapter/edit',
					del_url: 'subject/chapter/del',
					table: 'subject_chapter',
				},
			});

			const table = $('#table');

			table.bootstrapTable({
				// 请求地址
				url: $.fn.bootstrapTable.defaults.extend.index_url,
				queryParams: function (params) {
					console.log(params);
					const filter = JSON.parse(params.filter);
					// 如果id存在则添加进过滤条件
					if (ids) {
						filter.subid = ids;
						params.filter = JSON.stringify(filter);
					}
					return params;
				},
				// 设置工具栏元素
				toolbar: '.toolbar',
				// 设置主键
				pk: 'id',
				// 排序字段
				sortName: 'createtime',
				// 默认排序方式
				sortOrder: 'desc',
				showExport: false,
				// 渲染表格数据
				columns: [
					{ checkbox: true },
					{ field: 'id', title: 'ID', operate: false },
					{ field: 'title', title: __('ChapterTitle'), operate: false },
					{
						field: 'url',
						title: __('Video'),
						operate: false,
						formatter(value, row, index) {
							return `<a href="${video_playback}?url=${encodeURIComponent(value)}" target="_blank">${__(
								'videoPlayback'
							)}</a>`;
						},
					},
					{
						field: 'createtime',
						title: __('CreateTime'),
						operate: 'RANGE',
						addclass: 'datetimerange',
						sortable: true,
						formatter: Table.api.formatter.datetime,
					},
					{
						field: 'operate',
						title: __('Operate'),
						table: table,
						events: Table.api.events.operate,
						formatter: Table.api.formatter.operate,
					},
				],
			});

			Table.api.bindevent(table);
		},
		add() {
			controller.api.bindevent();
		},
		edit() {
			controller.api.bindevent();
		},
		del() {
			controller.api.bindevent();
		},
		api: {
			//JS模块化的全局方法
			bindevent: function () {
				Form.api.bindevent($('form[role=form]'));
			},
		},
	};

	return controller;
});
